package PageUtils

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Number"
)

func GetPageList(entityList []commEntity.IEntity, body map[string]interface{}) map[string]interface{} {
	data := make(map[string]interface{})
	data["total"] = len(entityList)

	// 根据ID排序
	commEntity.SortEntityList(entityList)

	if Map.Has(body, "pageNum") && Map.Has(body, "pageSize") {
		resultList := make([]commEntity.IEntity, 0)

		pageNum := Number.MakeInt(body["pageNum"])
		pageSize := Number.MakeInt(body["pageSize"])
		pageStartId := pageSize * (pageNum - 1)
		pageEndId := pageSize * pageNum
		index := 0

		for _, entity := range entityList {
			if index < pageStartId {
				index++
				continue
			}
			if index >= pageEndId {
				break
			}

			resultList = append(resultList, entity)
			index++
		}

		data["list"] = commEntity.BuildMapList(resultList)
	} else {
		data["list"] = commEntity.BuildMapList(entityList)
	}

	return data
}

func GetEntityPageList(entityList []commEntity.IEntity, pageNum, pageSize int) []commEntity.IEntity {
	resultList := make([]commEntity.IEntity, 0)

	pageStartId := pageSize * (pageNum - 1)
	pageEndId := pageSize * pageNum
	index := 0

	for _, entity := range entityList {
		if index < pageStartId {
			index++
			continue
		}
		if index >= pageEndId {
			break
		}

		resultList = append(resultList, entity)
		index++
	}

	return resultList
}

func GetMapPageList(entityList []map[string]interface{}, pageNum, pageSize int) map[string]interface{} {
	data := make(map[string]interface{})
	data["total"] = len(entityList)

	resultList := make([]map[string]interface{}, 0)

	pageStartId := pageSize * (pageNum - 1)
	pageEndId := pageSize * pageNum
	index := 0

	for _, entity := range entityList {
		if index < pageStartId {
			index++
			continue
		}
		if index >= pageEndId {
			break
		}

		resultList = append(resultList, entity)
		index++
	}

	data["list"] = resultList

	return data
}
